package com.zfei.gfb.controller;

import com.google.common.collect.Lists;
import com.zfei.gfb.api.TalkService;
import com.zfei.gfb.api.domain.TalkEvent;
import com.zfei.gfb.bean.Const;
import com.zfei.gfb.bean.Status;
import com.zfei.gfb.bean.StatusException;
import com.zfei.gfb.core.Auth;
import com.zfei.gfb.core.HttpJson;
import com.zfei.gfb.core.LogResult;
import com.zfei.gfb.dao.GfbGreenPlotApplyMapper;
import com.zfei.gfb.dao.GfbGreenPlotMapper;
import com.zfei.gfb.model.GfbGreenPlot;
import com.zfei.gfb.model.GfbGreenPlotApply;
import com.zfei.gfb.query.GfbGreenPlotApplyQuery;
import com.zfei.gfb.request.GfbGetRequest;
import com.zfei.gfb.request.GfbH5ClaimRequest;
import com.zfei.gfb.util.Beans;
import com.zfei.gfb.util.Gsons;
import com.zfei.gfb.util.Now;
import com.zfei.gfb.util.gson.Types;
import com.zfei.gfb.vo.GfbGreenPlotApplyListVo;
import com.zfei.gfb.vo.GfbGreenPlotApplyVo;
import com.zfei.gfb.vo.GfbGreenPlotVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author
 * @version 1.0
 */
@Api(tags = "H5菜地认领列表接口")
@RestController
@RequestMapping("/h5/plot/apply")
public class H5GreenPlotApplyController {

    @Resource
    private GfbGreenPlotApplyMapper gfbGreenPlotApplyMapper;

    @Resource
    private GfbGreenPlotMapper gfbGreenPlotMapper;

    @Resource
    private TalkService talkService;


    @ApiOperation(value = "详情")
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = GfbGreenPlotApplyVo.class),
    })
    @LogResult
    @HttpJson("/get")
    public Object get(@RequestBody GfbH5ClaimRequest request) {
        request.checkGetNull();
        String eventId = request.getEventId();

        TalkEvent talkEvent = talkService.getEvent(eventId);
        if (talkEvent == null) {
            throw new StatusException(Status.NOT_EXIST);
        }
        GfbGreenPlotApplyVo vo = Gsons.fromJson(talkEvent.getContent(), GfbGreenPlotApplyVo.class);

        if (vo.getPlotId() != null) {
            GfbGreenPlot greenPlot = gfbGreenPlotMapper.selectByPrimaryKey(vo.getPlotId());

            GfbGreenPlotVo greenPlotVo = Beans.copy(greenPlot, GfbGreenPlotVo.class);
            List<String> images = Gsons.fromJson(greenPlot.getImages(), Types.StringListType);
            greenPlotVo.setImages(images);

            vo.setGreenPlotVo(greenPlotVo);
        }
        return vo;
    }

}
